﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ListeningSystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        PerformanceCounter[] counters = null;//cpu 集合
        int coreCount = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            counters = new PerformanceCounter[System.Environment.ProcessorCount];
            for (int i = 0; i < counters.Length; i++)
            {
                counters[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
            }

            foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get())
            {
                coreCount += int.Parse(item["NumberOfCores"].ToString());
            }
            Console.WriteLine("Number Of Cores: {0}", coreCount);

            //int cupCount = 0;
            //int memoryCount = 0;

            //string strFilePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            //if (!File.Exists(strFilePath))
            //{
            //    File.Create(strFilePath);
            //}
            //System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.OpenOrCreate);
            //System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
            //sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + cupCount + "\t" + memoryCount);
            //sw.Close();
            //fs.Close();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
             this.timer1.Start();
            this.btnStart.Enabled = false;
            this.btnStop.Enabled = true;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();
            this.btnStart.Enabled = true;
            this.btnStop.Enabled = false;
        }



        private void timer1_Tick_1(object sender, EventArgs e)
        {
 
            long memoryNum = (SystemInfo.Instance.PhysicalMemory - SystemInfo.Instance.MemoryAvailable) * 100 / SystemInfo.Instance.PhysicalMemory;
            string msg = "";
            int memoryCount = Convert.ToInt32(memoryNum);
            if (memoryCount > 100 || memoryCount < 0)
            {
                memoryCount = 100;
                msg="内存使用占比:" + memoryCount.ToString() + ",";
      
            }
            else
            {
                msg="内存使用占比:" + memoryCount.ToString() + ",";
            }

           
            int cupCount = 0;
            if (counters.Length > 0)
            {
                float sumCpuValue = 0;
                for (int i = 0; i < counters.Length; i++)
                {
                    float f = counters[i].NextValue();
                    sumCpuValue += f;
                }

                int allcupCount = Convert.ToInt32(sumCpuValue);

                cupCount = allcupCount / coreCount;
                if (cupCount > 100 || cupCount < 0)
                {
                    cupCount = 100;
                    msg += "\t CPU使用占比:" + cupCount.ToString();   
                }
                else
                {
                    msg += "\t CPU使用占比:" + cupCount.ToString();
                   
                }

            }

      

            lblMsg.Text = "执行时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")+","+ msg;

            //labInfo.Text = msg;

            //SqlParameter[] paramters = new SqlParameter[] {
            //        new SqlParameter("@CpuCount",SqlDbType.Int),
            //        new SqlParameter("@MemoryCount",SqlDbType.Int),
            //        new SqlParameter("@CreateTime",SqlDbType.DateTime)
            //};

            //paramters[0].Value = cupCount;
            //paramters[1].Value = memoryCount;
            //paramters[2].Value = DateTime.Now;
            //SqlHelper.ExecuteNonQuery("insert into Sys_ListeningSystem(CpuCount,MemoryCount,CreateTime) values(@CpuCount,@MemoryCount,@CreateTime)", paramters);

            
            string strFilePath = AppDomain.CurrentDomain.BaseDirectory+DateTime.Now.ToString("yyyy-MM-dd")+".txt";
            if (!File.Exists(strFilePath))
            {
                File.Create(strFilePath);

                try
                {
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, true, System.Text.Encoding.UTF8);
                    sw.WriteLine("create_time\tcupCount\tmemoryCount");
                    sw.Close();
                }
                catch (Exception)
                {

               
                }
              
            }
            else
            {

                try
                {
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, true, System.Text.Encoding.UTF8);
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "\t" + cupCount + "\t" + memoryCount);
                    sw.Close();
       
                }
                catch (Exception)
                {
                    
                }

          
            }
            
        }
    }
}
